/**
 * Created by buhuan on 2017/3/16.
 */

var gaze = require('gaze');
var exec = require('child_process').exec;
var fs = require('fs')


function init(){
    //fs.mkdirSync('./apicontrollers');
    fs.mkdirSync('./wiki');
    //createConfigureFile();
    beginWatch();
}

// function createConfigureFile(){
//     var configure = {
//         "name": "API wiki",
//         "version": "0.0.1",
//         "description": "API文档说明",
//         "title": "API文档说明",
//         "url" : "http://localhost:8888",
//         "template":{
//             "forceLanguage":"zh-cn"
//         }
//     }
//     fs.writeFileSync('./wiki/apidoc.json',JSON.stringify(configure));
// }

//源代码监控路径
function beginWatch(){
    gaze('./apicontrollers/*.*',function(error,watcher){
        this.on('all', function(event, filepath) {
            console.log(filepath + ' was ' + event);
            runGeneartion();
        })
    });
}

function runGeneartion(){
    var com = exec('apidoc -i ./apicontrollers -o ./wiki ')
    com.stdout.on('data', function (data) {
        console.log("生成Api->"+data);
    });

    com.stderr.on('data', function (data) {
        console.log('生成错误啦->' + data);
    });
}

if(fs.existsSync('./wiki')){
    beginWatch();
}else{
    init();
}